$(function () {
    //**************************************加载列表轮播图
    function getList() {
        $.ajax({
            url: "/admin/swipers",
            type: "GET",
            success: function (res) {
                let src = res.data
                $("tbody").html("")
                $.each(src, function (index, item) {
                    let status = `${item.swiperstatus}`
                    if (status == 1) {
                        $("tbody").append(`<tr>
                     <td>${item.id}</td>
                     <td>
                         <img src="http://127.0.0.1:8888/uploads/${item.swiperimg}">
                     </td>
                     <td>${item.swiperimg}</td>
                     <td>
                         <!-- 在使用 -->
                         <span class="layui-badge layui-bg-green qiyong" del=${item.id}  dd=${item.swiperstatus}>√</span>
                     </td>
                     <td>
                         <button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete" del=${item.id}>
                             删除
                         </button>
                     </td>
                 </tr>`)
                    } else if (status == 2) {
                        $("tbody").append(`<tr>
                        <td>${item.id}</td>
                        <td>
                            <img src="http://127.0.0.1:8888/uploads/${item.swiperimg}">
                        </td>
                        <td>${item.swiperimg}</td>
                        <td>
                            <!-- 在使用 -->
                            <span class="layui-badge layui-bg-green layui-bg-cyan qiyong" del=${item.id}  dd=${item.swiperstatus}>x</span>
                        </td>
                        <td>
                            <button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete" del=${item.id}>
                                删除
                            </button>
                        </td>
                    </tr>`)
                    }




                })



            }
        })
    }
    getList()




    // *************************************轮播图状态切换  使用还是未使用
    $("tbody").on("click", ".qiyong", function () {
        let id1 = $(this).attr("del")
        let dd = parseInt($(this).attr("dd"))
        if (dd == 2) {
            $.ajax({
                url: "/admin/swipers/" + id1,
                type: "PUT",
                data: {
                    status: +1,
                },
                success: function () {
                    $(".qiyong").toggleClass("layui-bg-green")
                    $(".qiyong").html("√")
                    getList()

                }
            })
        } else if (dd == 1) {
            $.ajax({
                url: "/admin/swipers/" + id1,
                type: "PUT",
                data: {
                    status: +2,
                },
                success: function () {
                    $(".qiyong").toggleClass("layui-bg-cyan")
                    $(".qiyong").html("x")
                    getList()
                }
            })
        }


    })







    // ***************************************删除操作
    $("tbody").on("click", ".delete", function () {
        let id = $(this).attr("del")
        console.log(id);
        $.ajax({
            url: "/admin/swipers/" + id,
            type: "DELETE",
            success: function (res) {
                console.log(res);
                getList()
            }
        })
    })







    // ***************************************上传文件
    $('body').on('click', '#uploadSwiper', function () {
        $('#myfile').click()
    })

    // 监听文件选中事件
    $('body').on('change', '#myfile', function (e) {
        let files = e.target.files
        var fd = new FormData()
        $.each(files, function (index, item) {
            fd.append('swipers', item)
            console.log(item);
        })
        $.ajax({
            url: "/admin/swipers",
            type: "POST",
            data: fd,
            processData: false,
            contentType: false,
            success: function (res) {
                console.log(1);
                console.log(fd);
                console.log(res);
                getList()
            },
        })


    })
})